/*

*/


#ifndef __MEM_POOL_H__
#define __MEM_POOL_H__
#include <iostream>
#include "common_def.h"
#include <tr1/unordered_map>
using namespace std;
using namespace std::tr1;


//interface:
/*
   1.init(size_t)
   2.alloc(size_t)
   3.release(void *)
 */

namespace andrew
{
	namespace tools
	{
		namespace mem
		{
			//
			class mem_manager
			{
				public:
					size_t mem_idx;
					size_t used_size;
					size_t free_size;
					mem_manager *ptr_next;
			};
			class mem_pool
			{

			public:
				mem_pool();
				~mem_pool();
				int init(size_t _tmp_max_mem_size,
						size_t _tmp_chunk_size =MEM_POOL_DEFAULT_CHUNK_SIZE);
				char *alloc(size_t _tmp_alloc_size);
				int release(char* _ptr_mem);
			private:
				int initMemMgr();
			private:
				char *ptr_mem_pool;
				size_t chunk_size;
				char * ptr_bit_map;
				size_t bit_map_size;
				size_t max_mem_size;
				size_t free_count;//free
				size_t chunk_count;//total = free + used
				size_t used_count;//used
				bool b_inited;
				unsigned int curr_bit_map_idx;
				mem_manager *ptr_mem_mgr;
				unordered_map<unsigned int,unsigned int> hs_map_ptrs;
			};
		}
	}
}
#endif

